layui.define(['jquery', 'element'],
	function(exports) {
		"use strict";

		const $ = layui.jquery,
			layer = layui.layer,
			element = layui.element;

		let config;
		const localStorageTable='graphvis';
		const body = $('body');

		const pearAdmin = new function() {

			let configPath = '';

			//定义空的界面事件，未其他页面统一定义事件支持
			this.pageEvents={},
			
			this.setConfigPath = function(path) {
				configPath = path;
			}
			
			this.popupRight=function (param) {
				var self = this;
			    if (param.title == undefined) {
			        param.title = false;
			        param.closeBtn = false;
			    }
			    param.anim = -1;
			    param.fixed = true;
			    param.isOutAnim = false;
			    param.offset = 'r';
			    param.shadeClose = true;
			    param.area = param.area || ['360px', '100%'];
			    param.skin = 'layer-anim-right';
			    param.move = false;
			    return self.open(param);
			}
			
			this.popupLeft=function (param) {
				var self = this;
			    if (param.title == undefined) {
			        param.title = false;
			        param.closeBtn = false;
			    }
				param.anim = -1; 
				param.fixed = true;
			    param.isOutAnim = false;
			    param.offset = 'l';
			    param.shadeClose = true;
			    param.area = param.area || ['360px', '100%'];
			    param.skin = 'layer-anim-left';
			    param.move = false;
			    return self.open(param);
			}
			
			this.popupTop=function (param) {
				var self = this;
			    if (param.title == undefined) {
			        param.title = false;
			        param.closeBtn = false;
			    }
				param.anim = -1; 
			    param.fixed = true;
			    param.isOutAnim = false;
			    param.offset = 't';
			    param.shadeClose = true;
			    param.area = param.area || ['100%', '300px'];
			    param.skin = 'layer-anim-top';
			    param.move = false;
			    return self.open(param);
			}
			
			this.popupBottom=function (param) {
				var self = this;
			    if (param.title == undefined) {
			        param.title = false;
			        param.closeBtn = false;
			    }
				param.anim = -1; 
			    param.fixed = true;
			    param.isOutAnim = false;
			    param.offset = 'b';
			    param.shadeClose = true;
			    param.area = param.area || ['100%', '300px'];
			    param.skin = 'layer-anim-bottom';
			    param.move = false;
			    return self.open(param);
			}
			
			// 封装layer.open
			this.open=function (param) {
				var self = this;
			    if (!param.area) {
			        param.area = (param.type == 2) ? ['360px', '300px'] : '360px';
			    }
			    /* if (!param.offset) {
			        param.offset = 'r';
			    } */
			    if (param.fixed == undefined) {
			        param.fixed = false;
			    }
			    param.resize = param.resize != undefined ? param.resize : false;
			    param.shade = param.shade != undefined ? param.shade : .1;
			    var eCallBack = param.end;
			    param.end = function () {
			        eCallBack && eCallBack();
			    };
			    if (param.url) {
			        (param.type == undefined) && (param.type = 1);
			        var sCallBack = param.success;
			        param.success = function (layero, index) {
			            var dialogUrl = param.url;
			            $(layero).children('.layui-layer-content').load(dialogUrl, function () {
			                sCallBack ? sCallBack(layero, index) : '';
			            });
			        };
			    }
			    return layer.open(param);
			}
			
			this.btnLoading = function (elem, text, loading) {
			    if (text != undefined && (typeof text == "boolean")) {
			        loading = text;
			        text = undefined;
			    }
			    (loading == undefined) && (loading = true);
			    var $elem = $(elem);
			    if (loading) {
			        text && $elem.html(text);
			        $elem.find('.layui-icon').addClass('layui-hide');
			        $elem.addClass('icon-btn');
			        $elem.prepend('<i class="layui-icon layui-icon-loading layui-anim layui-anim-rotate layui-anim-loop ew-btn-loading"></i>');
			       /* $elem.prop('disabled', 'disabled'); */
			    } else {
			        $elem.find('.ew-btn-loading').remove();
			        /* $elem.removeProp('disabled', 'disabled'); */
			        if ($elem.find('.layui-icon.layui-hide').length <= 0) {
			            $elem.removeClass('icon-btn');
			        }
			        $elem.find('.layui-icon').removeClass('layui-hide');
			        text && $elem.html(text);
			    }
			}
			
			// 缓存临时数据
			this.putTempData= function (key, value) {
			    var tableName = localStorageTable + '_tempData';
			    if (value != undefined && value != null) {
			        layui.sessionData(tableName, {key: key, value: value});
			    } else {
			        layui.sessionData(tableName, {key: key, remove: true});
			    }
			}
			// 获取缓存临时数据
			this.getTempData= function (key) {
			    var tableName = localStorageTable + '_tempData';
			    var tempData = layui.sessionData(tableName);
			    if (tempData) {
			        return tempData[key];
			    } else {
			        return false;
			    }
			}
		};

		//页面自定义点击事件响应
		body.on('click', '*[page-event]', function () {
		    var event = $(this).attr('page-event');
		    var te = pearAdmin.pageEvents[event];
		    te && te.call(this, $(this));
		});
		
		exports('admin', pearAdmin);
	});
